package com.util.net;

import java.security.Security;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class Email {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Email sendmail = new Email();
		// sendmail.setHost("smtp.163.com");
		// sendmail.setUserName("zuaa");
		// sendmail.setPassWord("7394287");
		// sendmail.setTo("zuac@hotmail.com");
		// sendmail.setFrom("zuaa@163.com");
		// sendmail.setSubject("1111��");
		// sendmail.setContent("22222");
		// sendmail.attachfile("H:\\zuaatemp\\1230884322453.jpg");
		// sendmail.attachfile("H:\\zuaatemp\\1.txt");
		// sendmail.sendMail();

		
//		gmail
		sendmail.setUserName("xuping@xu-ping.cn");
		sendmail.setPassWord("zuaa@428");
		sendmail.setTo("345029460@qq.com");
		sendmail.setFrom("xuping@xu-ping.cn");
		sendmail.setHost("smtp.gmail.com");
		sendmail.setSubject("1111");
		sendmail.setContent("22222");
		sendmail.attachfile("d:/1.jpg");
		sendmail.attachfile("d:/1.jpg");
		sendmail.sendSslMail();

 
		
		
	}

	String to = "";
	String from = "";
	String host = "";
	String username = "";
	String password = "";
	String filename = "";
	String subject = "";
	String content = "";
	Vector file = new Vector();

	public Email(String to, String from, String smtpServer, String username,
			String password, String subject, String content) {
		this.to = to;
		this.from = from;
		this.host = smtpServer;
		this.username = username;
		this.password = password;
		this.subject = subject;
		this.content = content;
	}

	public Email() {
	}

	public void setHost(String host) {
		this.host = host;
	}

	public void setPassWord(String pwd) {
		this.password = pwd;
	}

	public void setUserName(String usn) {
		this.username = usn;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String transferChinese(String strText) {
		try {
			strText = MimeUtility.encodeText(new String(strText.getBytes(),
					"GB2312"), "GB2312", "B");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strText;
	}

	public void attachfile(String fname) {
		file.addElement(fname);
	}

	public boolean sendSslMail() {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", host);
		props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", "465");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.auth", "true");

		Session session = Session.getDefaultInstance(props,
				new Authenticator() {

					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});
		try {
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, address);
			subject = transferChinese(subject);
			msg.setSubject(subject);

			Multipart mp = new MimeMultipart();

			MimeBodyPart mbpContent = new MimeBodyPart();
			//mbpContent.setText(content);
			mbpContent.setContent(content, "text/html;charset=GBK");
			mp.addBodyPart(mbpContent);

			Enumeration efile = file.elements();
			while (efile.hasMoreElements()) {

				MimeBodyPart mbpFile = new MimeBodyPart();
				filename = efile.nextElement().toString();
				FileDataSource fds = new FileDataSource(filename);
				mbpFile.setDataHandler(new DataHandler(fds));
				mbpFile.setFileName(fds.getName());
				mp.addBodyPart(mbpFile);

			}
			file.removeAllElements();
			msg.setContent(mp);
			msg.setSentDate(new Date());
			Transport.send(msg);

		} catch (MessagingException mex) {
			mex.printStackTrace();
			Exception ex = null;
			if ((ex = mex.getNextException()) != null) {
				ex.printStackTrace();
			}
			return false;
		}
		return true;
	}

	public boolean sendMail() {
		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");

		Session session = Session.getDefaultInstance(props,
				new Authenticator() {

					public PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});
		try {
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, address);
			subject = transferChinese(subject);
			msg.setSubject(subject);

			Multipart mp = new MimeMultipart();

			MimeBodyPart mbpContent = new MimeBodyPart();
			mbpContent.setContent(content, "text/html;charset=GBK");
			mp.addBodyPart(mbpContent);

			Enumeration efile = file.elements();
			while (efile.hasMoreElements()) {

				MimeBodyPart mbpFile = new MimeBodyPart();
				filename = efile.nextElement().toString();
				FileDataSource fds = new FileDataSource(filename);
				mbpFile.setDataHandler(new DataHandler(fds));
				mbpFile.setFileName(fds.getName());
				mp.addBodyPart(mbpFile);

			}
			file.removeAllElements();
			msg.setContent(mp);
			msg.setSentDate(new Date());
			Transport.send(msg);

		} catch (MessagingException mex) {
			mex.printStackTrace();
			Exception ex = null;
			if ((ex = mex.getNextException()) != null) {
				ex.printStackTrace();
			}
			return false;
		}
		return true;
	}

}
